package com.ruoyi.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.cg.pojo.*;
import com.ruoyi.common.core.cg.result.Queryparam;
import com.ruoyi.manage.mapper.StoreMapper;
import com.ruoyi.manage.mapper.StoreVoMapper;
import com.ruoyi.manage.service.IStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class StoreServiceImpl implements IStoreService {
    @Autowired
    StoreMapper storeMapper;
    @Autowired
    StoreVoMapper storeVoMapper;
    @Override
    public PageInfo<Store> selectStoreList(Queryparam queryparam) {
        PageHelper.startPage(queryparam.getPageNum(),queryparam.getPageSize());
        List<Store> list = storeMapper.findAll(queryparam);
        PageInfo<Store> pageInfo = new PageInfo<>(list);
        for (Store store1 : list) {
            store1.setLeaderStores(storeMapper.LEADER_STORES(store1.getId()));
            store1.setLeader(storeMapper.selectleader(store1.getId()));
            store1.setOrder_store(storeMapper.selectorderstore(store1.getId()));
            store1.setUserOrder(storeMapper.selectorder(store1.getId()));

            //计算金额
            if(store1.getPrice()==null){
                store1.setPrice(BigDecimal.valueOf(0.00));
            }else {
                store1.setPrice(storeMapper.sumPrice(store1.getId()));
            }

            store1.setUser(storeMapper.selectuser(store1.getId()));
            store1.setGroup(storeMapper.selectgroup(store1.getId()));
            store1.setOrder(storeMapper.orderNum(store1.getId()));

            store1.setTypeName(storeMapper.Selectclasss(store1.getId()));
        }
        System.err.println(pageInfo.getTotal());
        return pageInfo;
    }

    @Override
    public Store byName(Integer name) {
        Store store = storeMapper.byName(name);

        Leader leader = storeMapper.LEADER(store.getId());
        if(leader!=null){
            store.setInvitationCode(leader.getInvitationCode());
            store.setIdcard(leader.getIdcard());
            store.setLname(leader.getName());
            store.setPhone(leader.getPhone());
        }

        Community community = storeMapper.selectCommunity(store.getId());
        if(community!=null){
            store.setCommunityName(community.getTitle());
        }

        Image image = storeVoMapper.getImage(store.getId());
        if(image!=null){
            store.setFrontIdCard(image.getFrontIdCard());
            store.setSideIdCard(image.getSideIdCard());
            store.setLicense(image.getLicense());
            store.setFoodHygieneApproval(image.getFoodHygieneApproval());
        }
        return store;
    }

    @Override
    public List<Classification> Selectclass() {
        List<Classification> classifications = storeMapper.Selectclass();
        return classifications;
    }

    @Override
    public Leader SelectLead(Integer id) {
        Leader leader = storeMapper.selectLeader(id);
        return leader;
    }

    @Override
    public boolean updateStore(Store store) {
        Leader leader = new Leader();
        if(leader!=null){
            leader.setId(store.getId());
            leader.setIdcard(store.getIdcard());
            leader.setName(store.getLname());
            leader.setInvitationCode(store.getInvitationCode());
            leader.setPhone(store.getPhone());
            storeMapper.updateLeader(leader);
        }else{
            return false;
        }

        Community community = new Community();
        if(community!=null){
            community.setId(store.getId());
            community.setTitle(store.getCommunityName());
            storeMapper.sommunityUpdate(community);
        }else{
            return false;
        }

        Store store1 = new Store();
        store1.setId(store.getId());
        store1.setName(store.getName());
        store1.setAddress(store.getAddress());
        store1.setPrice(store.getPrice());
        store1.setType(store.getType());
        store1.setStatus(store.getStatus());

        return storeMapper.updateStore(store1);
    }



}
